﻿using System;
using System.Linq;
using SharpChannel.Rpc;
using SharpErp.Data.Service;
using System.Collections;
using System.Configuration;
using SharpChannel.Core;

namespace SharpErp.Service.Client
{
    public class ServiceWarpper
    {
    	private static ISharpErpService service;

        public static ISharpErpService GetService()
        {
            if (service == null)
            {
                //service = new Data.Service.SharpErpServiceImpl();
                Hashtable hashTable = ConfigurationManager.GetSection("sharp-erp") as Hashtable;
                if(hashTable.ContainsKey("mode") && "cs".Equals(hashTable["mode"]))
                {
                    service = SharpChannelClient<ISharpErpService>.GetInstance();
                }
                else
                {
                    var session = new ClientSession(null);
                    var impl = new Data.Service.SharpErpServiceImpl();
                    impl.GetCurrentSession += new SharpErpServiceImpl.GetCurrentSessionDelegate(delegate { return session; });
                    service = impl;
                }
                if(service == null || !"ping".Equals(service.ping())){
                    service = null;
                    throw new Data.Common.Exceptions.ConnectFailedException("connect to server failed!");
                }
            }
            return service;
        }

        public static void Main(string[] args)
        {
            Console.WriteLine(GetService().GetAllItemClass().Count());
            Console.ReadKey();
        }
    }
}
